package com.jy.event.util;

import org.apache.commons.codec.binary.Base64;

import java.util.Map;


/**
 * @author zeming.fan@swiftpass.cn
 */
public class SignUtil {

    /**
     * 私钥
     */

    private static final String mchPrivateKey = "MIIEvAIBADANBgkqhkiG9w0BAQEFAASCBKYwggSiAgEAAoIBAQDD29aOY/CvAr1BfkFmlDxjuF7K/1EiUWlLsF1v74J4BhAe2n8db5vYKubTXrrIaPxUBZiKkdB0jU4zfVdnUfpdXZ+bARXvHygAn/iQO4truzH0WTuAc724Co+/yP/pTd0luwDK5qmM7GV9Z8JBZ0jZtRCL4WIZg41V/ANhZeZ/YDteSNGeS11JjhJk79CFVuwLstNB+Zb8FrY9a2HdDWa5eRvuYIC4LMiIu1qqTOGL8MENHYq2j1dCrU2HIcLuO3Cd/KOJXx8+ZJwsgO0XIfxb2VZYR8RuEXy/AYGws/x9sDFRkRi6w99iAZTMPtIiySdAyOVXHHKLLKzNtsDDynUtAgMBAAECggEAd9uO37CVV+SLQQsspsPMeRIZII1Cb4f2shUJ+ThPVXgQYXFBHRhx/MW0YGG2WHK3CAwsn5BASzM2ShhIZrgqSPzEca0maSFd75P2Hv3xgkoadfgti5xI29nKwokU4wd9cFuSTpj95Xm9WhMnM7MrR9+gcpnI2OhCk/4Xa4Fan1OIrQjdGj4JQycsE6I4TUEWGB57ZEvNpCpGUNR/RA/f0LqZNh2K8NaBLZpcsgnjmvelO/cKqRiiQYsjcOITricXuj3duo9/X+3ObrRbBwEpPiTnTGxqEW9viB/B9FSgqx+ScBLNLdy94JfIpHh1SZxY5u5/96R6kZqW6W3wiGIdwQKBgQDiWuLib6f40E8ovl8wh3rtPeAujbaHUwu92ublqKcoFwIOvWU3wRRZ4Ql55d+/XfvVdUBLlNm5gWT/vC8llnKXvWJ2A3HYIkkz8J/FDPUVUvmQ5WMRhzj4OMU5tdjrTdhooGhIVQexH7/FAemXIqyNlY53AM6CEVYbm8g8PenzeQKBgQDdgn0DwRJb83QZ+myMVcir59Lm3cHUYx+CCQ9Ny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Aw8p1LQIDAQAB";

    private static  final  String testKey="h92Tz0wF6QX66hR4pM7VVdXD4NNS3cSO";
    /**
     * @param signType 加密方式
     * @param preStr   加密参数
     * @param key      支付密钥（md5 加密需要）
     * @return
     */
    public static String getSign(String signType, String preStr, String key) {
        if ("RSA_1_256".equals(signType)) {
            try {
                return SignUtil.sign(preStr, "RSA_1_256", testKey);
            } catch (Exception e1) {
                // TODO Auto-generated catch block
                e1.printStackTrace();
            }
        } else {
            return MD5.sign(preStr, "&key=" + key, "utf-8");
        }
        return "";
    }

    //对返回参数的验证签名

    /**
     * TODO 目前为校验
     *
     * @param sign
     * @param signType
     * @param resultMap
     * @param key
     * @return
     * @throws Exception
     */
    public static boolean verifySign(String sign, String signType, Map<String, String> resultMap, String key) throws Exception {
        if ("RSA_1_256".equals(signType)) {
            Map<String, String> Reparams = SignUtils.paraFilter(resultMap);
            StringBuilder Rebuf = new StringBuilder((Reparams.size() + 1) * 10);
            SignUtils.buildPayParams(Rebuf, Reparams, false);
            String RepreStr = Rebuf.toString();
            if (SignUtil.verifySign(RepreStr, sign, "RSA_1_256", platPublicKey)) {
                return true;
            }
        } else if ("MD5".equals(signType)) {
            if (SignUtils.checkParam(resultMap, key)) {
                return true;
            }
        }
        return false;
    }

    public static boolean verifySign(String preStr, String sign, String signType, String platPublicKey) throws Exception {
        // 调用这个函数前需要先判断是MD5还是RSA
        // 商户的验签函数要同时支持MD5和RSA
        RSAUtil.SignatureSuite suite = null;

        if ("RSA_1_1".equals(signType)) {
            suite = RSAUtil.SignatureSuite.SHA1;
        } else if ("RSA_1_256".equals(signType)) {
            suite = RSAUtil.SignatureSuite.SHA256;
        } else {
            throw new Exception("不支持的签名方式");
        }

        boolean result = RSAUtil.verifySign(suite, preStr.getBytes("UTF8"), Base64.decodeBase64(sign.getBytes("UTF8")),
                platPublicKey);

        return result;
    }

    public static String sign(String preStr, String signType, String mchPrivateKey) throws Exception {
        RSAUtil.SignatureSuite suite = null;
        if ("RSA_1_1".equals(signType)) {
            suite = RSAUtil.SignatureSuite.SHA1;
        } else if ("RSA_1_256".equals(signType)) {
            suite = RSAUtil.SignatureSuite.SHA256;
        } else {
            throw new Exception("不支持的签名方式");
        }
        byte[] signBuf = RSAUtil.sign(suite, preStr.getBytes("UTF8"),
                mchPrivateKey);
        return new String(Base64.encodeBase64(signBuf), "UTF8");
    }
}
